<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-14
 * Time: 上午11:42
 */
class concern extends API_Controller
{
    protected $rules = array(
        "concern" => array(
            array(
                'field' => 'rid',
                'label' => '关注用户',
                'rules' => 'trim|required|numeric|callback_rid_check',
            ),
        )
    );
    public function __construct()
    {
        parent::__construct();
        $this->_auto();
        $this->load->model('concern_fans_model','mconcern_fans');
    }
    //关注列表
    public function clist() {
        $id = isset($this->data['id']) ? $this->data['id'] : $this->userinfo['id'];
        $where = array('uid'=>$id,'is_deleted'=>1);
        // 初始化翻页
        $fields = 'boc_concern_fans.id,boc_concern_fans.is_deleted,boc_account.nickname,boc_account.photo,boc_account.id as uid';
        $this->_list();
        $this->orderby = 'boc_concern_fans.created desc';
        if ($list = $this->mconcern_fans->get_list($this->limit, $this->offset, $this->orderby, $where, $fields,false,array('boc_account','boc_account.id=boc_concern_fans.rid','left'))) {
            foreach($list as $key=> &$row) {
                if(!$row['uid']) {unset($list[$key]);};
                $fans = $this->mconcern_fans->get_one(array('uid'=>$this->userinfo['id'],'rid'=>$row['uid'],'is_deleted'=>1));
                $row['is_fans'] = $fans ?  1: 0;
            }
            photo2url($list);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $list;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    //关注修改
    public function cupdate() {
        $this->form_validation->set_rules($this->rules['concern']);
        // validate验证结果
        if ($this->form_validation->run('', $this->data) == false) {
            // 返回失败
            $this->vdata['returnCode'] = '0011';
            $this->vdata['returnInfo'] = $this->trim_validation_errors();
        } else {
            $where = array('uid' => $this->userinfo['id'],'rid' => $this->data['rid']);
            $data = $this->mconcern_fans->get_one($where,'is_deleted');
            $account = $this->macc->get_one($this->userinfo['id']);
            $raccount = $this->macc->get_one($this->data['rid']);
            if($data && $data['is_deleted']) {
                $info = $this->mconcern_fans->update(array('is_deleted'=>0,'is_new'=>0),$where);
                $this->macc->set($this->userinfo['id'],array('concerns'=>$account['concerns'] -1));
                $this->macc->set($this->data['rid'],array('fans'=>$raccount['fans'] -1));
            } elseif($data && !$data['is_deleted']) {
                $info = $this->mconcern_fans->update(array('is_deleted'=>1,'is_new'=>1),$where);
                $this->macc->set($this->userinfo['id'],array('concerns'=>$account['concerns'] +1));
                $this->macc->set($this->data['rid'],array('fans'=>$raccount['fans'] +1));
            } else {
                $data = array(
                    'uid'=>$this->userinfo['id'],
                    'rid'=>$this->data['rid'],
                    'is_deleted'=>1,
                    'created'=>time(),
                );
                $info = $this->mconcern_fans->create($data);
                $this->macc->set($this->userinfo['id'],array('concerns'=>$account['concerns'] +1));
                $this->macc->set($this->data['rid'],array('fans'=>$raccount['fans'] +1));
            }
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $info;
        }

        $this->_send_json($this->vdata);
    }
    //添加关注
    public function cadd() {
        $data = array(
            'uid'=>$this->userinfo['id'],
            'rid'=>$this->data['rid'],
            'is_deleted'=>1,
            'created'=>time(),
        );
        $where = array('uid' => $this->userinfo['id'],'rid' => $this->data['rid'],'is_deleted'=>0);
        $info = $this->mconcern_fans->get_one($where,'id');
        $cdata = '';
        if(!$info) {
            $cdata = $this->mconcern_fans->create($data);
            $this->macc->set($this->userinfo['id'],array('concerns'=>$this->userinfo['concerns'] +1));
            $this->macc->set($this->data['rid'],array('fans'=>$this->userinfo['fans'] +1));
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $cdata;
        } else {
            if($info && $info['is_deleted']) {
                $info = $this->mconcern_fans->update(array('is_deleted'=>0),$where);
                $this->macc->set($this->userinfo['id'],array('concerns'=>$this->userinfo['concerns'] -1));
                $this->macc->set($this->data['rid'],array('fans'=>$this->userinfo['fans'] -1));
            } elseif($info && !$info['is_deleted']) {
                $info = $this->mconcern_fans->update(array('is_deleted'=>1),$where);
                $this->macc->set($this->userinfo['id'],array('concerns'=>$this->userinfo['concerns'] +1));
                $this->macc->set($this->data['rid'],array('fans'=>$this->userinfo['fans'] +1));
            } else {
                $data = array(
                    'uid'=>$this->userinfo['id'],
                    'rid'=>$this->data['rid'],
                    'is_deleted'=>1,
                    'created'=>time(),
                );
                $cdata = $this->mconcern_fans->create($data);
                $this->macc->set($this->userinfo['id'],array('concerns'=>$this->userinfo['concerns'] +1));
                $this->macc->set($this->data['rid'],array('fans'=>$this->userinfo['fans'] +1));
            }
            $this->vdata['returnCode'] = '00011';
            $this->vdata['returnInfo'] = '您已添加过，请不要重复添加';
            $this->vdata['content'] = $cdata;
        }
        $this->_send_json($this->vdata);
    }
    //我的粉丝
    public function cfens() {
        $id = $this->data['id'] ? $this->data['id'] : $this->userinfo['id'];
        $where = array('rid'=>$id,'is_deleted'=>1);
        $this->mconcern_fans->update(array('is_new'=>0),array('rid'=>$id,'is_new'=>1));
        // 初始化翻页
        $fields = 'boc_concern_fans.id,boc_concern_fans.is_deleted,boc_account.nickname,boc_account.photo,boc_account.id as uid';
        $this->_list();
        $this->orderby = 'boc_concern_fans.created desc';
        if ($list = $this->mconcern_fans->get_list($this->limit, $this->offset, $this->orderby, $where, $fields,false,array('boc_account','boc_account.id=boc_concern_fans.uid','left'))) {
            foreach($list as $key=> &$row) {
                if(!$row['uid']) {unset($list[$key]);};
                $fans = $this->mconcern_fans->get_one(array('uid'=>$this->userinfo['id'],'rid'=>$row['uid'],'is_deleted'=>1));
                $row['is_fans'] = $fans ?  1: 0;
            }
            photo2url($list);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $list;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }
        // 返回json数据
        $this->_send_json($this->vdata);
    }
    public function rid_check($str) {
        // 查找用户
        if ($info = $this->macc->get_one(array('id' => $str,'audit' => 0))) {
            if($str == $this->userinfo['id']) {
                $this->form_validation->set_message('rid_check', '请不要自己关注自己');
                return false;
            }
            return true;
        } else {
            $this->form_validation->set_message('rid_check', '此用户不存在或已被禁用');
            return false;
        }
        return false;
    }
    public function friends() {
        $uids = array();
        $rids = array();
        $users = $this->macc->get_all(array('concerns >'=>10,'audit'=>0,'id <> '=>$this->userinfo['id']),'id');
        if($users) {
            foreach($users as $row) {
                $uids[] = $row['id'];
            }
        }
        $concerns = $this->mconcern_fans->get_all(array('uid'=>$this->userinfo['id'],'is_deleted'=>1),'rid');
        $count_concerns = count($concerns);
        if($concerns && $count_concerns > 10) {
            foreach($concerns as $val) {
                $rids[] = $val['rid'];
            }
        }
        $ids = array();
        if($uids && $rids) {
            $ids = $this->mconcern_fans->get_similarity_uids($uids, $rids);
        }
        $phones = isset($this->data['phones']) ? $this->data['phones'] : '';
        $phone_ids = $this->mconcern_fans->get_phone_ids($phones, $this->userinfo, $ids);
        $phone_ids = assoc_unique( $phone_ids,'id');
        $info = array();
        foreach ($phone_ids as $row) {
            if($row['id'] == $this->userinfo['id']) continue;
            $user = $this->macc->get_one($row['id'], 'photo,uname');
            $user['uid'] = $row['id'];
            $user['title'] = $row['title'];
            $fans = $this->mconcern_fans->get_one(array('uid' => $this->userinfo['id'], 'rid' => $row['id']), 'is_deleted');
            $user['is_attention'] = $fans ? $fans['is_deleted'] : 0;
            $info[] = $user;
        }
        if($info) {
            photo2url($info);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $info;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }
        $this->_send_json($this->vdata);
    }
    public function friend() {
        $phones = isset($this->data['phones']) ? $this->data['phones'] : '';
        $phone_ids = $this->mconcern_fans->get_phone_id($phones, $this->userinfo['phone']);
        $phone_ids = assoc_unique( $phone_ids,'id');
        $info = array();
        foreach ($phone_ids as $row) {
            $user = $this->macc->get_one($row['id'], 'photo,uname');
            $user['uid'] = $row['id'];
            $user['title'] = $row['title'];
            $fans = $this->mconcern_fans->get_one(array('uid' => $this->userinfo['id'], 'rid' => $row['id']), 'is_deleted');
            $user['is_attention'] = $fans ? $fans['is_deleted'] : 0;
            $info[] = $user;
        }
        if($info) {
            photo2url($info);
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作成功';
            $this->vdata['content'] = $info;
        } else {
            $this->vdata['returnCode'] = '200';
            $this->vdata['returnInfo'] = '操作失败';
        }
        $this->_send_json($this->vdata);
    }
    public function friendsKw() {
        $uids = array();
        $rids = array();
        $kw = $this->data['kw'];
        if($kw) {
            $kw_uname = $this->macc->get_all(array('audit'=>0,'status'=>2,'like'=>array('uname',$kw)),'id');
            $kw_ids = array();
            foreach($kw_uname as $k => $v) {
                $kw_ids[] = $v['id'];
            }
            if($kw_ids) {
                $users = $this->macc->get_all(array('concerns >'=>10,'audit'=>0,'id <> '=>$this->userinfo['id']),'id');
                if($users) {
                    foreach($users as $row) {
                        $uids[] = $row['id'];
                    }
                }
                $concerns = $this->mconcern_fans->get_all(array('uid'=>$this->userinfo['id'],'is_deleted'=>1),'rid');
                $count_concerns = count($concerns);
                if($concerns && $count_concerns > 10) {
                    foreach($concerns as $val) {
                        $rids[] = $val['rid'];
                    }
                }
                $ids = array();
                if($uids && $rids) {
                    $ids = $this->mconcern_fans->get_similarity_uids($uids, $rids);
                }
                $phones = isset($this->data['phones']) ? $this->data['phones'] : '';
                $phone_ids = $this->mconcern_fans->get_phone_ids($phones, $this->userinfo, $ids);
                $info = array();
                foreach ($phone_ids as $row) {
                    if(!in_array($row['id'],$kw_ids)) continue;
                    $user = $this->macc->get_one($row['id'], 'photo,uname');
                    $user['uid'] = $row['id'];
                    $user['title'] = $row['title'];
                    $fans = $this->mconcern_fans->get_one(array('uid' => $this->userinfo['id'], 'rid' => $row['id']), 'is_deleted');
                    $user['is_attention'] = $fans ? $fans['is_deleted'] : 0;
                    $info[] = $user;
                }
                if($info) {
                    photo2url($info);
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作成功';
                    $this->vdata['content'] = $info;
                } else {
                    $this->vdata['returnCode'] = '200';
                    $this->vdata['returnInfo'] = '操作失败';
                }
            } else {
                $this->vdata['returnCode'] = '10006';
                $this->vdata['returnInfo'] = '查询结果为空';
            }
        } else {
            $this->vdata['returnCode'] = '10006';
            $this->vdata['returnInfo'] = '请输入用户名关键词';
        }

        $this->_send_json($this->vdata);
    }

}